﻿using UnityEngine;

public class UIPanelEx: UIPanel
{
    public bool enableNotch;
    public bool fixSizeByHomeIndicator;

    public override Vector3[] GetSides (Transform relativeTo)
    {
        Vector3[] result = base.GetSides(relativeTo);

        if (enableNotch)
        {
            Vector4 viewportMargins = NotchInfoManager.GetViewportMargins();
            float width = result[2].x - result[0].x;
            float height = result[1].y - result[3].y;

            result[0].x += width * viewportMargins.x;
            result[2].x -= width * viewportMargins.z;
            result[1].y -= height * viewportMargins.w;
            result[3].y += height * viewportMargins.y;
        }

        if (fixSizeByHomeIndicator)
        {
            if (this.anchorCamera != null)
            {
                float screenMargin = NotchInfoManager.GetHomeIndicatorScreenMargin();
                float halfScreenHeight = screenMargin;
                Vector3 worldPos = this.anchorCamera.ScreenToWorldPoint(new Vector3(Screen.width / 2, halfScreenHeight, -this.anchorCamera.transform.position.z));
                Vector3 uiVector = this.transform.InverseTransformVector(worldPos);

                if (result[3].y < uiVector.y)
                {
                    result[3].y = uiVector.y;
                }
            }
        }

        float width2 = result[2].x - result[0].x;
        float height2 = result[1].y - result[3].y;

        Vector3 scale = this.transform.localScale;
        scale.x = 1 - scale.x;
        scale.y = 1 - scale.y;

        result[0].x += width2 * scale.x / 2;
        result[2].x -= width2 * scale.x / 2;
        result[1].y -= height2 * scale.y / 2;
        result[3].y += height2 * scale.y / 2;

        return result;
    }
}